home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / SHDK_2 / SHDATPK.PAS < prev    next >
Pascal/Delphi Source File  |  1992-04-30  |  16KB  |  519 lines

  1. {$I SHDEFINE.INC}
  2.  
  3. {$I SHUNITSW.INC}
  4.  
  5. {$D-,L-}
  6. unit ShDatPk;
  7. {
  8.                                 ShDatPk
  9.  
  10.                         A Date Manipulation Unit
  11.  
  12.                                    by
  13.  
  14.                               Bill Madison
  15.  
  16.                    W. G. Madison and Associates, Ltd.
  17.                           13819 Shavano Downs
  18.                             P.O. Box 780956
  19.                        San Antonio, TX 78278-0956
  20.                              (512)492-2777
  21.                              CIS 73240,342
  22.  
  23.                   Copyright 1991 Madison & Associates
  24.                           All Rights Reserved
  25.  
  26.         This file may  be used and distributed  only in accord-
  27.         ance with the provisions described on the title page of
  28.                   the accompanying documentation file
  29.                               SKYHAWK.DOC
  30. }
  31.  
  32. interface
  33.  
  34. uses
  35.   shUtilPk,
  36.   Dos;
  37.  
  38. type
  39.   GregType  = record
  40.                 Year  : LongInt;
  41.                 Month,
  42.                 Day   : byte;
  43.                 end;
  44.   TimeType  = record
  45.                 H,
  46.                 M,
  47.                 S   : byte;
  48.                 end;
  49.  
  50. const
  51.   DayStr  : array[0..6] of string[9] =
  52.                         ('Sunday', 'Monday', 'Tuesday', 'Wednesday',
  53.                          'Thursday', 'Friday', 'Saturday');
  54.  
  55.   MonthStr: array[1..12] of string[9] =
  56.                         ('January',   'February', 'March',    'April',
  57.                          'May',       'June',     'July',     'August',
  58.                          'September', 'October',  'November', 'December');
  59.  
  60. function DoW(Greg : GregType) : byte;
  61.              {computes the day of the week (Sunday = 0; Saturday = 6)
  62.              from the Gregorian date.}
  63.  
  64. function Greg2ANSI(G : GregType) : string;
  65. {Returns the date as an ANSI date string (YYYYMMDD)}
  66.  
  67. function Greg2JDate(Greg : GregType) : integer;
  68.              {computes the Julian date from the Gregorian date.}
  69.  
  70. function Greg2JDN(Greg : GregType) : LongInt;
  71.              {computes the Julian Day-Number from the Gregorian date.}
  72.  
  73. procedure JDate2Greg(JDate, Year : Integer;
  74.                   var Greg : GregType);
  75.              {computes the Gregorian date from the Julian date.}
  76.  
  77. function JDN2ANSI(JDN : LongInt) : string;
  78. {Returns the JDN as an ANSI date string (YYYYMMDD)}
  79.  
  80. procedure JDN2Greg(JDN : LongInt;
  81.                   var Greg : GregType);
  82.              {computes the Gregorian date from the Julian Day-Number.}
  83.  
  84. function Greg2Str(G : GregType; Delim : string) : string;
  85. {Returns a Gregorian date record as a string of the form MMdDDdYYYY,
  86.  where the separator, "d", is Delim[1].}
  87.  
  88. function JDN2Str(JDN : LongInt; Delim : string) : string;
  89. {Returns a Julian Day-Number as a MMdDDdYYYY string.}
  90.  
  91. function Now  : LongInt;
  92. {Returns the system time as Seconds-Since-Midnight.}
  93.  
  94. procedure Now2Time(var T : TimeType);
  95. {Returns the system time as a Time record.}
  96.  
  97. function NowStr(Delim : string; T24 : boolean) : string;
  98. {Returns the system time as a string of the form:
  99.           HHdMMdSSss    if Delim is non-empty and T24 (24 hour time) is
  100.                         false. The delimiter used, "d", is Delim[1]. The
  101.                         suffix, "ss", is "am" or "pm" as appropriate.
  102.           HHdMMdSS      if Delim is non-empty and T24 (24 hour time) is
  103.                         true. The delimiter used, "d", is Delim[1]. The
  104.                         time will be expressed in 24-hour form.
  105.           HHMMSSss      if Delim is empty and T24 (24 hour time) is
  106.                         false. The suffix, "ss", is "am" or "pm" as
  107.                         appropriate.
  108.           HHMM          if Delim is empty and T24 (24 hour time) is
  109.                         true. The time will be expressed in 24-hour form.
  110. }
  111.  
  112. procedure SSM2Time(SSM : LongInt; var T : TimeType);
  113. {Converts Seconds-Since-Midnight to a Time record.}
  114.  
  115. function SSM2TimeStr(SSM : LongInt; Delim : string; T24 : boolean) : string;
  116. {Returns Seconds-Since-Midnight as a string of the form:
  117.           HHdMMdSSss    if Delim is non-empty and T24 (24 hour time) is
  118.                         false. The delimiter used, "d", is Delim[1]. The
  119.                         suffix, "ss", is "am" or "pm" as appropriate.
  120.           HHdMMdSS      if Delim is non-empty and T24 (24 hour time) is
  121.                         true. The delimiter used, "d", is Delim[1]. The
  122.                         time will be expressed in 24-hour form.
  123.           HHMMSSss      if Delim is empty and T24 (24 hour time) is
  124.                         false. The suffix, "ss", is "am" or "pm" as
  125.                         appropriate.
  126.           HHMM          if Delim is empty and T24 (24 hour time) is
  127.                         true. The time will be expressed in 24-hour form.
  128. }
  129.  
  130. function Time2SSM(T : TimeType) : LongInt;
  131. {Returns a Time record as Seconds-Since-Midnight.}
  132.  
  133. function Time2TimeStr(T : TimeType; Delim : string; T24 : boolean) : string;
  134. {Returns a Time record as a string of the form:
  135.           HHdMMdSSss    if Delim is non-empty and T24 (24 hour time) is
  136.                         false. The delimiter used, "d", is Delim[1]. The
  137.                         suffix, "ss", is "am" or "pm" as appropriate.
  138.           HHdMMdSS      if Delim is non-empty and T24 (24 hour time) is
  139.                         true. The delimiter used, "d", is Delim[1]. The
  140.                         time will be expressed in 24-hour form.
  141.           HHMMSSss      if Delim is empty and T24 (24 hour time) is
  142.                         false. The suffix, "ss", is "am" or "pm" as
  143.                         appropriate.
  144.           HHMM          if Delim is empty and T24 (24 hour time) is
  145.                         true. The time will be expressed in 24-hour form.
  146. }
  147.  
  148. function Today  : LongInt;
  149. {Returns the system date as a Julian Day-Number}
  150.  
  151. function Today2ANSI : string;
  152. {Returns the system date as an ANSI date string (YYYYMMDD)}
  153.  
  154. procedure Today2Greg(var G : GregType);
  155. {Returns the system date as a Gregorian date record.}
  156.  
  157. function TodayStr(Delim : string) : string;
  158. {Returns the system date as a string of the form MMdDDdYYYY, where the
  159.  separator, "d", is Delim[1].}
  160.  
  161. implementation
  162.  
  163. const
  164.   D0 =    1461;
  165.   D1 =  146097;
  166.   D2 = 1721119;
  167.  
  168. function Greg2JDN(Greg : GregType) : LongInt;
  169. var
  170.   Century,
  171.   XYear    : LongInt;
  172. begin {Greg2JDN}
  173.   with Greg do begin
  174.     If Month <= 2 then begin
  175.       Year := pred(Year);
  176.       Month := Month + 12;
  177.       end;
  178.     Month := Month - 3;
  179.     Century := Year div 100;
  180.     XYear := Year mod 100;
  181.     Century := (Century * D1) shr 2;
  182.     XYear := (XYear * D0) shr 2;
  183.     Greg2JDN := ((((Month * 153) + 2) div 5) + Day) + D2
  184.                                       + XYear + Century;
  185.     end; {with Greg}
  186.   end; {Greg2JDN}
  187.  
  188.  
  189. {**************************************************************}
  190.  
  191. procedure JDN2Greg(JDN : LongInt;
  192.                   var Greg : GregType);
  193. var
  194.   Temp,
  195.   XYear   : LongInt;
  196.   YYear,
  197.   YMonth,
  198.   YDay    : Integer;
  199. begin {JDN2Greg}
  200.   with Greg do begin
  201.     Temp := (((JDN - D2) shl 2) - 1);
  202.     XYear := (Temp mod D1) or 3;
  203.     JDN := Temp div D1;
  204.     YYear := (XYear div D0);
  205.     Temp := ((((XYear mod D0) + 4) shr 2) * 5) - 3;
  206.     YMonth := Temp div 153;
  207.     If YMonth >= 10 then begin
  208.       YYear := YYear + 1;
  209.       YMonth := YMonth - 12;
  210.       end;
  211.     YMonth := YMonth + 3;
  212.     YDay := Temp mod 153;
  213.     YDay := (YDay + 5) div 5;
  214.     Year := YYear + (JDN * 100);
  215.     Month := YMonth;
  216.     Day := YDay;
  217.     end; {with Greg}
  218.   end; {JDN2Greg}
  219.  
  220.  
  221. {**************************************************************}
  222.  
  223. function Greg2JDate(Greg : GregType) : integer;
  224. var
  225.   G     : GregType;
  226. begin {Greg2JDate}
  227.   with G do begin
  228.     Year := Greg.Year;
  229.     Month := 1;
  230.     Day := 1;
  231.     end; {with G}
  232.   Greg2JDate := Greg2JDN(Greg) - Greg2JDN(G) + 1;
  233.   end; {Greg2JDate}
  234.  
  235.  
  236. {**************************************************************}
  237.  
  238. procedure JDate2Greg(JDate, Year : Integer;
  239.                   var Greg : GregType);
  240. var
  241.   G     : GregType;
  242. begin
  243.   with G do begin
  244.     Year := Greg.Year;
  245.     Month := 1;
  246.     Day := 1;
  247.     end; {with G}
  248.   JDN2Greg((Greg2JDN(G) + JDate - 1), Greg);
  249.   end; {JDate2Greg}
  250.  
  251.  
  252. {**************************************************************}
  253.  
  254. function DoW(Greg : GregType) : byte;
  255.              {computes the day of the week (Sunday = 0; Saturday = 6)
  256.              from the Gregorian date.}
  257. begin
  258.   DoW := (Greg2JDN(Greg) + 1) mod 7;
  259.   end; {DayOfWeek}
  260.  
  261. {**************************************************************}
  262.  
  263. procedure Today2Greg(var G : GregType);
  264. {Returns the system date as a Gregorian date record.}
  265.   var
  266.     R : registers;
  267.   begin
  268.     with R do begin
  269.       AH := $2A;
  270.       MsDos( R );
  271.       with G do begin
  272.         Year  := CX;
  273.         Month := DH;
  274.         Day   := DL;
  275.         end; {with G}
  276.       end; {with R}
  277.     end; {Today2Greg}
  278.  
  279. function Today  : LongInt;
  280. {Returns the system date as a Julian Day-Number}
  281.   var
  282.     G : GregType;
  283.   begin
  284.     Today2Greg(G);
  285.     Today := Greg2JDN(G);
  286.     end; {Today}
  287.  
  288. function Greg2Str(G : GregType; Delim : string) : string;
  289. {Returns a Gregorian date record as a string of the form MMdDDdYYYY,
  290.  where the separator, "d", is Delim[1].}
  291.   var
  292.     S1: string[4];
  293.     S2: string;
  294.     D : char;
  295.   begin
  296.     if Length(Delim) = 0 then
  297.       D := #0
  298.     else
  299.       D := Delim[1];
  300.     with G do begin
  301.       str(Month:2, S2); {Month}
  302.       str(Day:2, S1); {Day}
  303.       S2 := S2 + D + S1;
  304.       str(Year:4, S1); {Year}
  305.       S2 := S2 + D + S1;
  306.       end; {with R}
  307.     Greg2Str := RepAllF(DelAllF(S2, #0), ' ', '0');
  308.     end; {Greg2Str}
  309.  
  310. function Greg2ANSI(G : GregType) : string;
  311. {Returns the date as an ANSI date string (YYYYMMDD)}
  312.   var
  313.     S1: string[4];
  314.     S2: string;
  315.   begin
  316.     with G do begin
  317.       str(Year:4, S2);  {Year}
  318.       str(Month:2, S1); {Month}
  319.       S2 := S2 + S1;
  320.       str(Day:2, S1);   {Day}
  321.       S2 := S2 + S1;
  322.       end; {with G}
  323.     Greg2ANSI := RepAllF(S2, ' ', '0');
  324.     end; {Greg2ANSI}
  325.  
  326. function JDN2ANSI(JDN : LongInt) : string;
  327. {Returns the JDN as an ANSI date string (YYYYMMDD)}
  328.   var
  329.     G : GregType;
  330.   begin
  331.     JDN2Greg(JDN, G);
  332.     JDN2ANSI := Greg2ANSI(G);
  333.     end; {JDN2ANSI}
  334.  
  335. function Today2ANSI : string;
  336. {Returns the system date as an ANSI date string (YYYYMMDD)}
  337.   begin
  338.     Today2ANSI := JDN2ANSI(Today);
  339.     end; {Today2ANSI}
  340.  
  341. function JDN2Str(JDN : LongInt; Delim : string) : string;
  342. {Returns a Julian Day-Number as a MMdDDdYYYY string.}
  343.   var
  344.     G : GregType;
  345.   begin
  346.     JDN2Greg(JDN, G);
  347.     JDN2Str := Greg2Str(G, Delim);
  348.     end; {JDN2Str}
  349.  
  350. function TodayStr(Delim : string) : string;
  351. {Returns the system date as a string of the form MMdDDdYYYY, where the
  352.  separator, "d", is Delim[1].}
  353.   var
  354.     G : GregType;
  355.   begin
  356.     Today2Greg(G);
  357.     TodayStr := Greg2Str(G, Delim);
  358.     end; {TodayStr}
  359.  
  360. function Time2SSM(T : TimeType) : LongInt;
  361. {Returns a Time record as Seconds-Since-Midnight.}
  362.   var
  363.     L1,
  364.     L2,
  365.     L3 : LongInt;
  366.   begin
  367.     with T do begin
  368.       L1 := H;
  369.       L2 := M;
  370.       L3 := S;
  371.       Time2SSM := (3600 * L1) + (60 * L2) + L3;
  372.       end; {with T}
  373.     end; {Time2SSM}
  374.  
  375. function Now  : LongInt;
  376. {Returns the system time as Seconds-Since-Midnight.}
  377.   var
  378.     R : registers;
  379.     T : TimeType;
  380.   begin
  381.     with R do begin
  382.       AH := $2C;
  383.       MsDos( R );
  384.       with T do begin
  385.         H := CH;
  386.         M := CL;
  387.         S := DH;
  388.         end; {with T}
  389.       end; {with R}
  390.       Now := Time2SSM(T);
  391.     end; {Now}
  392.  
  393. procedure SSM2Time(SSM : LongInt; var T : TimeType);
  394. {Converts Seconds-Since-Midnight to a Time record.}
  395.   var
  396.     Q : LongInt;
  397.     R : byte;
  398.   begin
  399.     with T do begin
  400.       Q := SSM div 60;
  401.       S := SSM mod 60;  {Get SECONDS}
  402.       H := Q div 60;    {Get HOURS}
  403.       M := Q mod 60;    {Get MINUTES}
  404.       end; {with T}
  405.     end; {SSM2Time}
  406.  
  407. procedure Now2Time(var T : TimeType);
  408. {Returns the system time as a Time record.}
  409.   begin
  410.     SSM2Time(Now, T);
  411.     end; {Now2Time}
  412.  
  413. function Time2TimeStr(T : TimeType; Delim : string; T24 : boolean) : string;
  414. {Returns a Time record as a string of the form:
  415.           HHdMMdSSss    if Delim is non-empty and T24 (24 hour time) is
  416.                         false. The delimiter used, "d", is Delim[1]. The
  417.                         suffix, "ss", is "am" or "pm" as appropriate.
  418.           HHdMMdSS      if Delim is non-empty and T24 (24 hour time) is
  419.                         true. The delimiter used, "d", is Delim[1]. The
  420.                         time will be expressed in 24-hour form.
  421.           HHMMSSss      if Delim is empty and T24 (24 hour time) is
  422.                         false. The suffix, "ss", is "am" or "pm" as
  423.                         appropriate.
  424.           HHMM          if Delim is empty and T24 (24 hour time) is
  425.                         true. The time will be expressed in 24-hour form.
  426. }
  427.  var
  428.     S1: string[2];
  429.     S2: string;
  430.     AP: string[2];
  431.     D : char;
  432.   begin
  433.     if Length(Delim) = 0 then
  434.       D := #0
  435.     else
  436.       D := Delim[1];
  437.     with T do begin
  438.       if not T24 then
  439.         case H of
  440.           0     : begin
  441.                     H := 12;
  442.                     AP := 'am';
  443.                     end;
  444.           1..11 : begin
  445.                     AP := 'am';
  446.                     end;
  447.           12    : begin
  448.                     AP := 'pm';
  449.                     end;
  450.           13..23: begin
  451.                     H := H - 12;
  452.                     AP := 'pm';
  453.                     end;
  454.           end {case}
  455.       else
  456.         AP := '';
  457.       str(H:2, S2);
  458.       str(M:2, S1);
  459.       S2 := S2 + D + S1;
  460.       if (not T24) or (D <> #0) then begin
  461.         str(S:2, S1);
  462.         S2 := S2 + D + S1;
  463.         end;
  464.       end; {with R}
  465.     Time2TimeStr := RepAllF(DelAllF(S2, #0), ' ', '0') + AP;
  466.     end; {Time2TimeStr}
  467.  
  468. function NowStr(Delim : string; T24 : boolean) : string;
  469. {Returns the system time as a string of the form:
  470.           HHdMMdSSss    if Delim is non-empty and T24 (24 hour time) is
  471.                         false. The delimiter used, "d", is Delim[1]. The
  472.                         suffix, "ss", is "am" or "pm" as appropriate.
  473.           HHdMMdSS      if Delim is non-empty and T24 (24 hour time) is
  474.                         true. The delimiter used, "d", is Delim[1]. The
  475.                         time will be expressed in 24-hour form.
  476.           HHMMSSss      if Delim is empty and T24 (24 hour time) is
  477.                         false. The suffix, "ss", is "am" or "pm" as
  478.                         appropriate.
  479.           HHMM          if Delim is empty and T24 (24 hour time) is
  480.                         true. The time will be expressed in 24-hour form.
  481. }
  482.   var
  483.     R : Registers;
  484.     T : TimeType;
  485.   begin
  486.     with R do begin
  487.       AH := $2C;
  488.       MsDos( R );
  489.       with T do begin
  490.         H := CH;
  491.         M := CL;
  492.         S := DH;
  493.         NowStr := Time2TimeStr(T, Delim, T24);
  494.         end; {with T}
  495.       end; {with R}
  496.     end;{NowStr}
  497.  
  498. function SSM2TimeStr(SSM : LongInt; Delim : string; T24 : boolean) : string;
  499. {Returns Seconds-Since-Midnight as a string of the form:
  500.           HHdMMdSSss    if Delim is non-empty and T24 (24 hour time) is
  501.                         false. The delimiter used, "d", is Delim[1]. The
  502.                         suffix, "ss", is "am" or "pm" as appropriate.
  503.           HHdMMdSS      if Delim is non-empty and T24 (24 hour time) is
  504.                         true. The delimiter used, "d", is Delim[1]. The
  505.                         time will be expressed in 24-hour form.
  506.           HHMMSSss      if Delim is empty and T24 (24 hour time) is
  507.                         false. The suffix, "ss", is "am" or "pm" as
  508.                         appropriate.
  509.           HHMM          if Delim is empty and T24 (24 hour time) is
  510.                         true. The time will be expressed in 24-hour form.
  511. }
  512.   var
  513.     T : TimeType;
  514.   begin
  515.     SSM2Time(SSM, T);
  516.     SSM2TimeStr := Time2TimeStr(T, Delim, T24);
  517.     end; {SSM2TimeStr}
  518.   end.
  519.